package cn.fl.spl.controller;

import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ISplAppOpinionReactionService;
import cn.fl.spl.vo.SplAppOpinionReactionVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysUlFilesRecVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.base.page.PaginationVo;
import fl.mongo.facade.spl.condition.SplAppOpinionReactionQC;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 用户建议反馈表 控制层
 */
@Controller
@RequestMapping("/spl/splAppOpinionReaction")
public class SplAppOpinionReactionController extends BaseController {

	/**
	 * SplAppOpinionReactionService 服务层BEAN
	 */
	@Autowired
	private ISplAppOpinionReactionService splAppOpinionReactionService;

	/**
	 * 文件 服务层BEAN
	 */
	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("spl/splAppOpinionReaction/splAppOpinionReactionList");
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String phone = request.getParameter("phone");
		String usrRlNm = request.getParameter("usrRlNm");
		String crtTmMin = request.getParameter("crtTmMin");
		String crtTmMax = request.getParameter("crtTmMax");
		PaginationVo paginationVo = new PaginationVo();
		if (StringUtils.isNotEmpty(pageNo)) {
			paginationVo.setPageNo(Integer.parseInt(pageNo));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			paginationVo.setPageSize(Integer.parseInt(pageSize));
		}

		SplAppOpinionReactionQC qc = new SplAppOpinionReactionQC();
		if (StringUtils.isNotEmpty(phone)) {
			qc.setPhone(phone);
		}
		if (StringUtils.isNotEmpty(usrRlNm)) {
			qc.setUsrRlNm(usrRlNm);
		}
		if (StringUtils.isNotEmpty(crtTmMin)) {
			qc.setCrtTmMin(DateUtils.formatStringToDate(crtTmMin, DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(crtTmMax)) {
			qc.setCrtTmMax(DateUtils.formatStringToDate(crtTmMax, DateUtils.YYYY_MM_DD));
		}
		qc.setIsDel(BaseConstant.IS_YESNO_NO);
		qc.setIsValid(BaseConstant.IS_YESNO_YES);
		paginationVo.setCondition(qc);
		
		//TODO SOMETHING
		PaginationVo pagination = this.splAppOpinionReactionService.querySplAppOpinionReactionListPage(paginationVo);
		List<SplAppOpinionReactionVO> list = (List<SplAppOpinionReactionVO>)pagination.getDatas();
		ListPageVO<SplAppOpinionReactionVO> listPage = new ListPageVO<SplAppOpinionReactionVO>(list, Integer.parseInt(String.valueOf(pagination.getTotalCount())));
		BaseLogUtils.newWebLogger("查询数据完成. DATA:" + JSONObject.toJSONString(pagination))
				.setLogOperType(LogOperEnum.RESP)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();

		return listPage;
	}

	/**
	 * 删除多条数据
	 */
	@RequestMapping(value = "/removeByIds.do", method = {RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(ids)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.newWebLogger("删除数据,主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
			return result;
		}
		List<String> idsList = new ArrayList<String>();
		JSONArray idsArray = JSONArray.parseArray(ids);
		for (Object object : idsArray) {
			idsList.add(object.toString());
		}
		try {
			boolean success = this.splAppOpinionReactionService.removeByIds(idsList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.newWebLogger("删除数据失败,ID:" + idsList.toString()).setLogOperType(LogOperEnum.RESP)
						.setSysCd(CurrentThreadContext.getCurrentSysCd())
						.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
						.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
						.setWarning(false)
						.warn(LogExceptionEnum.COMMON_CODE_0002);
				return result;
			}
			BaseLogUtils.newWebLogger("删除数据.[id="+ids+"]")
					.setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();

		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("删除数据失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.newWebLogger("删除数据失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}

		return result;
	}
	

	/**
	 * 查看附件
	 */
	@RequestMapping(value = "/viewAttachments.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object viewAttachments(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES);

		SysUlFilesRecQueryCondition condition = new SysUlFilesRecQueryCondition();
		condition.setPkId(id);
		condition.setSysCd(SplSystemConstant.SPL_SYS_CD);
		condition.setCfgCd(SplSystemConstant.SHOP_APP_OPINION_CODE);
		condition.setOrgCd(SplSystemConstant.DEFAULT_ORG_CD);

		List<SysUlFilesRecVO> searchList = null;
		JSONArray jsonArray = null;
		try {
			searchList = this.sysUlFilesRecService.searchList(condition);
			BaseLogUtils.newWebLogger("查询附件 , 主键ID[pkId : " + id + "]")
					.setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();

			if (CollectionUtils.isNotEmpty(searchList)) {
				jsonArray = new JSONArray();

				for (SysUlFilesRecVO sysUlFilesRecVO : searchList) {
					jsonArray.add(sysUlFilesRecVO.getFilePath());
				}
			}

		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("查询失败");
			BaseLogUtils.newWebLogger("查询附件失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		result.setData(jsonArray);

		return result;
	}

}

